//
//  ReceiptViewController.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/27/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit

class ReceiptViewController: UIViewController {
    
    // MARK: - Outlets
    
    lazy private var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFont(ofSize: 24, weight: .medium)
        lbl.textColor = .black
        lbl.textAlignment = .left
        return lbl
    }()
    lazy private var receiptView: ReceiptView = {
        let rView = ReceiptView(historyItem: self.historyItem)
        rView.backgroundColor = .white
        rView.layer.borderWidth = 1
        rView.layer.borderColor = UIColor.mediumGreyBlue.cgColor
        rView.layer.cornerRadius = 10
        return rView
    }()
    lazy private var submitBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("Поделиться", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel!.font = UIFont.systemFont(ofSize: 17, weight: .medium)
        btn.backgroundColor = .blueColor
        btn.layer.cornerRadius = 15
        btn.addTarget(self, action: #selector(shareReceipt), for: .touchUpInside)
        return btn
    }()
    
    // MARK: - Properties
    
    internal var historyItem: HistoryItem!
    
    // MARK: - Actions
    
    /// dismiss current view controller
    @objc private func goBack (sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc private func shareReceipt () {
        let items = [URL(string: "https://www.auto-nomad.kz/receipt/\(historyItem.id)")!]
        let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
        present(ac, animated: true)
    }
    
    // MARK: - Life-Cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUpNavBar()
        self.addViews()
    }
    
    
    // MARK: - UI
    
    /// simple navigation bar with title, and custom back buttom
    private func setUpNavBar() {
        self.view.backgroundColor = .white
        self.navigationController?.navigationBar.defaultNavBar()
        let backIcon = UIImage(named: "cancel")
        let backButton = UIBarButtonItem(image: backIcon, landscapeImagePhone: backIcon, style: .plain, target: self, action: #selector(goBack(sender:)))
        backButton.tintColor = .greyColor
        self.navigationItem.simpleNavBar(title: "", backButton: backButton)
    }
    
    private func addViews () {
        self.view.addSubview(titleLbl)
        self.view.addSubview(receiptView)
        self.view.addSubview(submitBtn)
        
        titleLbl.text = "Квитанция"
        titleLbl.snp.makeConstraints { (make) in
            make.top.equalTo(self.view.safeAreaLayoutGuide).inset(21)
            make.leading.trailing.equalToSuperview().inset(16)
        }
        
        receiptView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLbl.snp.bottom).offset(20)
            //make.height.equalTo(200)
            make.leading.trailing.equalToSuperview().inset(16)
        }
        
        submitBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(view.safeAreaLayoutGuide).inset(20)
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(44)
        }
    }
    
}
